<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>根据首字查成语</title>
    <style>
        h1, b{
            color: red;
        }
        form>div:first-child{
            margin: 20px 0;
        }
        form>div:nth-child(2){
            display: flex;
        }
        input{
            padding: 6px;
        }
        input:last-child{
            margin-left: 10px;
        }
        .result{
            line-height: 2;
        }
    </style>
</head>
<body>
<h1>根据首字查成语</h1>
<?php if($_POST): ?>
    <div class="result">
        <?php
        $first=$_POST["first"]; //获取用户输入的成语的首字，比如“我”
        $sql="select * from chengyu where first=?"; //要执行的语句
        $showSql="select * from chengyu where first='<b>$first</b>'"; //显示的语句
        echo "<div>$showSql</div>";
        echo "<div>以【<b>$first</b>】开头的成语有：</div>";
        $db=new PDO("mysql:host=localhost;dbname=db","root","12qwas");
        $ps=$db->prepare($sql); //创建含占位符参数的语句对象，可以避免SQL注入漏洞
        $ps->bindValue(1, $first); //绑定参数值
        $ps->execute();//执行查询
        foreach($ps as $record){
            echo $record[0], " "; //显示查询到的程序
        }
        ?>
    </div>
<?php else: ?>
    <form  method="post">
        <div>请输入成语的第一个汉字~</div>
        <div>
            <input type="text" name="first" />
            <input type="submit" value="执行查询">
        </div>
    </form>
<?php endif ?>
</body>
</html>